package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.controller.LoginController;
import com.example.prison.entity.SystemLog;
import com.example.prison.service.SystemLogService;
import com.example.prison.mapper.SystemLogMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author jiang
* @description 针对表【system_log】的数据库操作Service实现
* @createDate 2023-06-12 15:57:49
*/
@Service
public class SystemLogServiceImpl extends ServiceImpl<SystemLogMapper, SystemLog>
    implements SystemLogService{

    @Autowired
    private SystemLogMapper systemLogMapper;

    @Override
    public PageInfo<SystemLog> get(int pageNumber, int pageSize, String operator,String beginTime,String endTime) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(systemLogMapper.get(operator,beginTime,endTime));
    }

    @Override
    public int add(String function) {
        SystemLog log = new SystemLog();
        log.setFunction(function);
        log.setOperationTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        if(ObjectUtils.isNotEmpty(LoginController.getOperator())) {
            log.setOperator(LoginController.getOperator());
        }
        systemLogMapper.add(log);
        return 1;
    }

    @Override
    public int add(String function, String userName) {
        SystemLog log = new SystemLog();
        log.setFunction(function);
        log.setOperationTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        log.setOperator(userName);
        systemLogMapper.add(log);
        return 1;
    }

}




